const { TreeNode, BinaryTreeTraversal } = require("../template")

function testBinaryTreeBasic() {
  const traversal = new BinaryTreeTraversal()

  // 构建测试树:    1
  //              /   \
  //             2     3
  //            / \   / \
  //           4   5 6   7
  const root = new TreeNode(1)
  root.left = new TreeNode(2)
  root.right = new TreeNode(3)
  root.left.left = new TreeNode(4)
  root.left.right = new TreeNode(5)
  root.right.left = new TreeNode(6)
  root.right.right = new TreeNode(7)

  console.log(
    "1. 前序遍历测试:",
    (() => {
      const result = traversal.preorder(root)
      return JSON.stringify(result) === JSON.stringify([1, 2, 4, 5, 3, 6, 7])
    })()
  )

  console.log(
    "2. 中序遍历测试:",
    (() => {
      const result = traversal.inorder(root)
      return JSON.stringify(result) === JSON.stringify([4, 2, 5, 1, 6, 3, 7])
    })()
  )

  console.log(
    "3. 后序遍历测试:",
    (() => {
      const result = traversal.postorder(root)
      return JSON.stringify(result) === JSON.stringify([4, 5, 2, 6, 7, 3, 1])
    })()
  )

  console.log(
    "4. 层序遍历测试:",
    (() => {
      const result = traversal.levelOrder(root)
      return JSON.stringify(result) === JSON.stringify([[1], [2, 3], [4, 5, 6, 7]])
    })()
  )

  console.log(
    "5. 单节点树遍历测试:",
    (() => {
      const singleNode = new TreeNode(42)
      return JSON.stringify(traversal.preorder(singleNode)) === JSON.stringify([42]) &&
             JSON.stringify(traversal.inorder(singleNode)) === JSON.stringify([42]) &&
             JSON.stringify(traversal.postorder(singleNode)) === JSON.stringify([42]) &&
             JSON.stringify(traversal.levelOrder(singleNode)) === JSON.stringify([[42]])
    })()
  )

  console.log(
    "6. 空树遍历测试:",
    (() => {
      return JSON.stringify(traversal.preorder(null)) === JSON.stringify([]) &&
             JSON.stringify(traversal.inorder(null)) === JSON.stringify([]) &&
             JSON.stringify(traversal.postorder(null)) === JSON.stringify([]) &&
             JSON.stringify(traversal.levelOrder(null)) === JSON.stringify([])
    })()
  )

  console.log(
    "7. this上下文遍历测试:",
    (() => {
      const traversalObj = {
        multiplier: 2,
        traversal: new BinaryTreeTraversal(),
        processTree: function(root) {
          const result = this.traversal.preorder(root)
          return result.map(value => value * this.multiplier)
        }
      }
      const result = traversalObj.processTree(root)
      return JSON.stringify(result) === JSON.stringify([2, 4, 8, 10, 6, 12, 14])
    })()
  )

  console.log(
    "8. 复杂对象this绑定测试:",
    (() => {
      const treeObj = {
        prefix: "node_",
        traversal: new BinaryTreeTraversal(),
        processWithPrefix: function(root) {
          const result = this.traversal.inorder(root)
          return result.map(value => this.prefix + value)
        }
      }
      const result = treeObj.processWithPrefix(root)
      return JSON.stringify(result) === JSON.stringify(["node_4", "node_2", "node_5", "node_1", "node_6", "node_3", "node_7"])
    })()
  )

  console.log(
    "9. 节点创建this上下文测试:",
    (() => {
      const nodeObj = {
        baseValue: 10,
        createNode: function(value) {
          return new TreeNode(this.baseValue + value)
        }
      }
      const customNode = nodeObj.createNode(5)
      return customNode.value === 15
    })()
  )
}

testBinaryTreeBasic()
